package cn.windy.samary.module.admin.controller;

import cn.windy.samary.module.admin.common.UserLine;
import cn.windy.samary.module.admin.entity.OperateLog;
import cn.windy.samary.module.admin.service.IOperateLogService;
import cn.windy.samary.utils.PageContent;
import cn.windy.samary.utils.Res;
import cn.windy.samary.utils.ShiroUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * 操作日志
 *
 * @author zyl
 * @date 2018-06-01 15:14
 */
@Controller
@RequestMapping("/admin/operateLog")
public class OperateLogController {

    @Resource
    private IOperateLogService operateLogService;

    @GetMapping
//    @OpLog(value = "打开操作日志主页")
    public String index() {

        return "admin/operatelog/operatelog";
    }

    @ResponseBody
    @GetMapping("list")
//    @OpLog("查询操作日志列表")
    public Res list(PageContent pageContent, String date) {

        Example example = new Example(OperateLog.class);
        Example.Criteria criteria = example.createCriteria();
        UserLine userLine = ShiroUtils.getUserLine();
        if (!"0001".equals(userLine.getId())) {
            criteria.andNotEqualTo("operateUserId", "0001");
        }
        String sort = pageContent.getSort();
        String order = pageContent.getOrder();
        if (StringUtils.isBlank(sort)) {
            sort = "createTime";
        }
        Example.OrderBy orderBy = example.orderBy(sort);
        if (!"asc".equals(order)) {
            orderBy.desc();
        }
        Page<Object> page = PageHelper.offsetPage(pageContent.getOffset(), pageContent.getLimit(), true);
        List<OperateLog> operateLogs = operateLogService.selectByExample(example);
        return Res.rows(operateLogs, page.getTotal());
    }
}
